package com.design.movieticket.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.movieticket.common.util.RandomTicketNum;
import com.design.movieticket.dto.OrdersDto;
import com.design.movieticket.entity.*;
import com.design.movieticket.mapper.*;
import com.design.movieticket.service.OrdersService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    private MovieArrangeMapper movieArrangeMapper;

    @Resource
    private UserCouponMapper userCouponMapper;

    @Resource
    private BalanceMapper balanceMapper;

    @Resource
    private IncomeMapper incomeMapper;

    /** 分页查询 */
    @Override
    public LambdaQueryWrapper buildQuery(Orders orders) {
        LambdaQueryWrapper lambdaQueryWrapper;
        if(orders.getStatus()==88){
            lambdaQueryWrapper = Wrappers.lambdaQuery(Orders.class)
                    .eq(StringUtils.isNotBlank(orders.getOrderNumber()), Orders::getOrderNumber, orders.getOrderNumber())
                    .in(Orders::getStatus, 0,1,2);
        }
        else{
            lambdaQueryWrapper = Wrappers.lambdaQuery(Orders.class)
                    .eq(StringUtils.isNotBlank(orders.getOrderNumber()), Orders::getOrderNumber, orders.getOrderNumber())
                    .in(Orders::getStatus, orders.getStatus());
        }
        return lambdaQueryWrapper;
    }

    /** 分页相关 */
    @Override
    public <E extends IPage<Orders>> E page(E page, Wrapper<Orders> queryWrapper) {
        return super.page(page, queryWrapper);
    }

    /** 新增或更新用户数据 */
    @Override
    public void addOrUpdateOrderInfo(Orders orders) {
        super.saveOrUpdate(orders);
    }

    /** 获取用户信息详情 */
    @Override
    public Orders getOrderDetail(long id) {
        return super.getById(id);
    }

    /** 删除用户信息 */
    @Override
    public void delOrderById(long id) {
        super.removeById(id);
    }


    @Override
    public String wxCheckOrderS(Orders orders,Long useCouponId) {
        String result = "购票成功";
        Balance balance = balanceMapper.selectOne(new LambdaQueryWrapper<Balance>().eq(Balance::getUsername,orders.getUsername()));
        if (balance.getBalance().compareTo(orders.getOrderPay())==-1){
            return "余额不足";
        }
        if (!useCouponId.equals(-1)){
            userCouponMapper.deleteById(useCouponId);
        }

        Income income = new Income();
        income.setIncomeMoney(orders.getOrderPay());
        income.setIncomeType("订单收入");
        income.setResource("影院分成");
        incomeMapper.insert(income);

        balance.setBalance(balance.getBalance().subtract(orders.getOrderPay())); //更改余额相关信息
        balance.setPoints(balance.getPoints().add(orders.getOrderPay()));
        balanceMapper.updateById(balance);
        orders.setOrderNumber(RandomTicketNum.createTicketNum());
        orders.setStatus(1);
        super.saveOrUpdate(orders);
        return result;
    }

    @Override
    public List<OrdersDto> wxGetOrdersByUsername(String username) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = format.format(date);
        return ordersMapper.wxGetOrdersByUserName(username,nowTime);
    }

    @Override
    public List<OrdersDto> wxGetHadBeenMovieByUsername(String username) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = format.format(date);
        return ordersMapper.wxGetHadBeenMovieByUserName(username,nowTime);
    }
}
